﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using Testing.Models;
using Silvermoon.Shapes;
using Silvermoon.Windows;
using Testing.Controls;
using Silvermoon.Images;
using Silvermoon.UI;
using Silvermoon.Windows.Styles;
using System.Globalization;
using System.Diagnostics;

namespace Testing.Pages
{
    public class DatePickerPage : DemoWindowBase
    {
        private Picker dayPicker;
        private Picker monthPicker;
        private Picker yearPicker;
        private Grid grid;
        private DatePickerModel model;
        private Toolbar toolbar;

        public DatePickerPage()
            : base()
        {
            InitializePage();
        }


        public DateTime Date
        {
            get { return GetDateFromPicker(); }
            set
            {
                SetPickerDate(value);
            }

        }

        private void SetPickerDate(DateTime value)
        {

            SetDays(value.Year, value.Month, value.Day);
        }

        private DateTime GetDateFromPicker()
        {
            int year = model.GetYearFromIndex(yearPicker.SelectedItemIndex);
            int month = model.GetMonthFromIndex(monthPicker.SelectedItemIndex);
            int day = model.GetDayFromIndex(dayPicker.SelectedItemIndex);
            return new DateTime(year, month, day);
        }

        protected override void Dispose(bool dispose)
        {
            base.Dispose(dispose);
            DatePickerItem.DisposeItems();
        }

        private void InitializePage()
        {
            SetTransition(300, TransitionMask.Fade | TransitionMask.Zoom, TransitionMask.Fade | TransitionMask.Explode);
            Title = "CHOOSE DATE";

            const PickerMode pm = PickerMode.Fade | PickerMode.AutoHide;
            Styles.SetClass<Picker>(new PickerStyle
            {
                ItemHeight = 140,
                Lense = Shape.Empty,
                IsCyclic = true,
                Mode = pm,
                Margin = Margin.Zero,
                TextColor = PhoneColors.White,

            });


            model = new DatePickerModel();

            dayPicker = new Picker { Margin = Margin.Zero };
            monthPicker = new Picker { Margin = Margin.Zero };
            yearPicker = new Picker { Margin = Margin.Zero };
            monthPicker.SelectedItemChanged += new EventHandler(OnSelectedItemChanged);
            yearPicker.SelectedItemChanged += new EventHandler(OnSelectedItemChanged);


            grid = new Grid { Margin = Margin.Zero };
            grid.Rows.AddRange(1f);
            grid.Columns.AddRange(2f, 2f, 2f);

            string pattern = CultureInfo.CurrentUICulture.DateTimeFormat.LongDatePattern;
            Trace.WriteLine(pattern);

            grid.SetControl(dayPicker, 0, 0);
            grid.SetControl(monthPicker, 1, 0);
            grid.SetControl(yearPicker, 2, 0);
            //grid.AdControl(model.GetDayIndex(), dayPicker);
            //grid.AddControl(model.GetMonthIndex(), monthPicker);
            //grid.AddControl(model.GetYearIndex(), yearPicker);
            Controls.Add(grid);

            if (Toolbar != null) Toolbar.Dispose();
            toolbar = new Toolbar();
            toolbar.Buttons.Add(new Button { Content = "Done", Command = Page.BackCommand, Width = 200 });
            toolbar.Buttons.Add(new Button { Content = "Cancel", Command = Page.BackCommand, Width = 200 });
            this.Toolbar = toolbar;
            BindModel();
            Date = DateTime.Now.Date;
        }

        void OnSelectedItemChanged(object sender, EventArgs e)
        {
            if (yearPicker.SelectedItemIndex >= 0 && monthPicker.SelectedItemIndex >= 0 && dayPicker.SelectedItemIndex >= 0)
            {
                int year = model.GetYearFromIndex(yearPicker.SelectedItemIndex);
                int month = model.GetMonthFromIndex(monthPicker.SelectedItemIndex);
                int day = model.GetDayFromIndex(dayPicker.SelectedItemIndex);
                SetDays(year, month, day);
            }
        }

        protected override void OnShown()
        {
            base.OnShown();
        }

        private void BindModel()
        {

            dayPicker.GetVirtualItem += OnGetPickerItem;
            monthPicker.GetVirtualItem += OnGetPickerItem;
            yearPicker.GetVirtualItem += OnGetPickerItem;

            monthPicker.ItemsSource = model.Months;
            yearPicker.ItemsSource = model.Years;

        }

        private void SetDays(int year, int month, int day)
        {
            monthPicker.SelectedItemIndex = model.GetIndexFromMonth(month);
            yearPicker.SelectedItemIndex = model.GetIndexFromYear(year);
            dayPicker.ItemsSource = model.GetDays(month, year);
            dayPicker.SelectedItemIndex = model.GetIndexFromDay(day);
        }

        void OnGetPickerItem(object sender, PickerItemEventArgs e)
        {
            var item = DatePickerItem.GetItem();
            item.DataValue = e.Value;
            e.Container = item;
        }
    }
}
